package com.project.util;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
//import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author(王思翰)
 * @Description(类的描述)
 * @Date(创建时间) 2021/9/9 17:11
 */
@RestController
@RequestMapping("test")
public class TestController {
    public final static String FILE_PATH = "C:/Users/pc/Desktop/图片/";

    @Autowired
    RabbitTemplate template;

    @PostMapping("ImgPost")
    public void ImgPost(@RequestParam("fileName") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        if (fileName.substring(fileName.lastIndexOf(".")).equals(".jpg") && fileName.length() > 0) {
            fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
            File newFile = new File(FILE_PATH + fileName);
            try {
                file.transferTo(newFile);
                String newImg = FILE_PATH + newFile.getName() +
                        "_test" + newFile.getName().substring(newFile.getName().indexOf("."));
                googleCompress(newFile.getAbsolutePath(), newImg);
                String str=ImgCompress.ImageToBase64ByLocal(newImg);
                System.out.println(str);
                template.convertAndSend("imgDirect","img.Direct",str);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }


    /**
     * 压缩图片
     * @param olderImg 文件绝对路径
     * @param newImg 压缩后的文件路径和文件名
     */
    public static void googleCompress(String olderImg, String newImg) {
        try {
            /**
             * scale图片长宽  outputQuality图片质量
             */
            Thumbnails.of(olderImg).scale(0.5f).outputQuality(0.5f).toFile(newImg);
            /**
             * 指定大小
             */
            Thumbnails.of(olderImg).size(300, 300).toFile(newImg);
            /**
             * keepAspectRatio(false)按比例
             */
//            Thumbnails.of(olderImg).size(120, 120).keepAspectRatio(false).toFile(newImg);
            /**
             * rotate(角度),正数：顺时针 负数：逆时针
             */
            // Thumbnails.of(olderImg).size(1280, 1024).rotate(90).toFile(newImg);
            /**
             *  watermark(位置，水印图，透明度)
             */
            Thumbnails.of(olderImg).size(1280, 1024).watermark(Positions.CENTER, ImageIO.read(new File(olderImg)), 0.5f).outputQuality(0.8f).toFile(newImg);
            /**
             * 裁剪
             */
            Thumbnails.of(olderImg).sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false).toFile(newImg);
            /**
             * 转换图像格式
             */
//             Thumbnails.of(olderImg).size(1280, 1024).outputFormat("png").toFile(newImg);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = "imgQueue")
    public void test(String str) throws IOException, InterruptedException {
        System.out.println("我进来了");
        System.out.println(str);
        System.out.println(ImgCompress.Base64ToImage(str, "D:/base64ImgTest/" + System.currentTimeMillis() + ".jpg"));
    }


}
